/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bice.filter;

import com.bice.backbean.SessionBean;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Sirius
 */
@WebFilter(filterName = "LoginFilter", urlPatterns = {"/*"})
public class LoginFilter implements Filter {
    
    private FilterConfig filterConfig = null;
    
    public LoginFilter() {
    }    

    /**
     *
     * @param request The servlet request we are processing
     * @param response The servlet response we are creating
     * @param chain The filter chain we are processing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest)request;
        HttpServletResponse httpResponse = (HttpServletResponse)response;
        HttpSession session = httpRequest.getSession(false);
        boolean logged = false;
        if(session != null) {
            SessionBean sessionBean = (SessionBean)session.getAttribute("sessionBean");
            if(sessionBean != null && sessionBean.isLogged()) {
                logged = true;
            }
        }
        if(logged) {
            if(httpRequest.getPathInfo() != null && httpRequest.getPathInfo().contains("register")) {
                httpResponse.sendRedirect(httpRequest.getContextPath() + "/faces/index.xhtml");
            } else {
                chain.doFilter(request, response);
            }
        } else if(httpRequest.getPathInfo() == null || httpRequest.getPathInfo().contains("resource")) {
            chain.doFilter(request, response);
        } else {
            if(!logged && (!httpRequest.getPathInfo().contains("index") && !httpRequest.getPathInfo().contains("register"))) {
                httpResponse.sendRedirect(httpRequest.getContextPath() + "/faces/index.xhtml");
            } else {
                chain.doFilter(request, response);
            }
        }
    }

    /**
     * Return the filter configuration object for this filter.
     */
    public FilterConfig getFilterConfig() {
        return (this.filterConfig);
    }

    /**
     * Set the filter configuration object for this filter.
     *
     * @param filterConfig The filter configuration object
     */
    public void setFilterConfig(FilterConfig filterConfig) {
        this.filterConfig = filterConfig;
    }

    /**
     * Destroy method for this filter
     */
    @Override
    public void destroy() {        
    }

    /**
     * Init method for this filter
     */
    @Override
    public void init(FilterConfig filterConfig) {        
        this.filterConfig = filterConfig;
    }

    /**
     * Return a String representation of this object.
     */
    @Override
    public String toString() {
        if (filterConfig == null) {
            return ("LoginFilter()");
        }
        StringBuilder sb = new StringBuilder("LoginFilter(");
        sb.append(filterConfig);
        sb.append(")");
        return (sb.toString());
    }

}